//
//  EMMessageFrame.m
//  EaseMob
//
//  Created by wwj on 16/5/31.
//  Copyright © 2016年 wfw. All rights reserved.
//

#import "EMMessageFrame.h"
#import "NSString+Extend.h"
#import "EMTextMessageBody.h"
#import "EMImageMessageBody.h"
#import <SDWebImageManager.h>

#define kPading 10
#define kVoicePading 20

@implementation EMMessageFrame
- (void)setMessage:(EMMessage *)message{
    _message = message;
    EMMessageBody *msgBody = message.body;
    
    //时间
    if (NO==_isHiden) {
        CGFloat timeX = 0;
        CGFloat timeY = 20;
        CGFloat timeW = kScreenW;
        CGFloat timeH = 30;
        _timeF=CGRectMake(timeX, timeY, timeW, timeH);
    }else{
        _timeF = CGRectZero;
    }
    
    //头像
    CGFloat iconW = 40;
    CGFloat iconH = 40;
    CGFloat iconY = CGRectGetMaxY(_timeF) + kPading;
    CGFloat iconX;
    if (_type == MessageTypeOther) {
        iconX = kPading;
    }else{
        iconX = kScreenW - kPading - iconW;
    }
    _iconF=CGRectMake(iconX, iconY, iconW, iconH);
    
    switch (msgBody.type) {
        case EMMessageBodyTypeText://文本类型
            [self sumText:msgBody];
            break;
        case EMMessageBodyTypeImage://图片类型
            [self sumImage:msgBody];
            break;
            
        case EMMessageBodyTypeLocation://位置类型
            
            break;
            
        case EMMessageBodyTypeVoice://语音类型
            [self sumVoice];
            break;
            
        case EMMessageBodyTypeVideo://位置类型
            
            break;
            
        case EMMessageBodyTypeFile://文件类型
            
            break;
            
        case EMMessageBodyTypeCmd://命令类型
            break;
        
    }
}

- (void)sumText:(EMMessageBody *)msgBody{
    
    EMTextMessageBody *textBody = (EMTextMessageBody *)msgBody;
    CGFloat textY=_iconF.origin.y;
    CGFloat textX;
    CGSize textSize=[textBody.text sizeWithFont:kTextFont maxSize:CGSizeMake(150, MAXFLOAT)];
    CGSize buttonSize=CGSizeMake(textSize.width+kBtnPading, textSize.height+kBtnPading);
    if (_type == MessageTypeOther) {
        textX = CGRectGetMaxX(_iconF)+kPading;
    }else{
        textX = _iconF.origin.x-kPading-buttonSize.width;
    }
    _textF = (CGRect){{textX,textY},buttonSize};
    _height = CGRectGetMaxY(_textF);
}

- (void)sumVoice{
    CGFloat voiceY = _iconF.origin.y;
    CGFloat voiceW = 100;
    CGFloat voiceX;
    if (_type == MessageTypeOther) {
        voiceX = CGRectGetMaxX(_iconF) + kPading;
    }else{
        voiceX = _iconF.origin.x - kPading - voiceW;
    }
    _voiceF = CGRectMake(voiceX, voiceY, voiceW, 50);
    _height = CGRectGetMaxY(_voiceF);
}

- (void)sumImage:(EMMessageBody *)msgBody{
    EMImageMessageBody *imageBody = (EMImageMessageBody *)msgBody;
    CGFloat imageY = _iconF.origin.y;
    CGFloat imageX;
    NSString *path = imageBody.thumbnailLocalPath;
    if (path.length == 0) {
        path = imageBody.localPath;
    }
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:path]];
    if (image) {
        CGSize imageSize = [self sumImageSize:image];
        if (_type == MessageTypeOther) {
            imageX = CGRectGetMaxX(_iconF) + kPading;
        }else{
            imageX = _iconF.origin.x - kPading - imageSize.width;
        }
        _imageF = CGRectMake(imageX, imageY, imageSize.width, imageSize.height);
        _height = CGRectGetMaxY(_imageF);
        
    }else{
        NSString *url = imageBody.thumbnailRemotePath;
        if (url.length == 0) {
            url = imageBody.remotePath;
        }
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_enter(group);
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:url] options:SDWebImageRetryFailed progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            if (finished) {
                dispatch_group_leave(group);
            }
        }];
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:url];
            CGSize imageSize = [self sumImageSize:image];
            CGFloat imageX;
            if (_type == MessageTypeOther) {
                imageX = CGRectGetMaxX(_iconF) + kPading;
            }else{
                imageX = _iconF.origin.x - kPading - imageSize.width;
            }
            _imageF = CGRectMake(imageX, imageY, imageSize.width, imageSize.height);
            _height = CGRectGetMaxY(_imageF);
        });
    }
   

    
}

- (CGSize)sumImageSize:(UIImage *)image{
    CGFloat imageW;
    CGFloat imageH;
    if (image) {
        CGFloat scal = image.size.height / image.size.width;
        if (scal < 1) {
            imageW = 100;
            imageH = scal * imageW;
        }else if (scal > 1 && scal < 2){
            imageW = 50;
            imageH = scal * imageW;
        }else if (scal > 2){
            imageW = 50;
            imageH  = imageW * 2;
        }
    }
    return CGSizeMake(imageW, imageH);
}

@end
